#include "stdafx.h"
#include "..\clientinterfaces.h"
#include ".\artnum.h"
#include "pageInfoGetter.hpp"
#include "screen_mgr.h"

std::map<std::string,ArtNum_Uvs> ArtNum::m_dic;
bool ArtNum::b_load_dic=false;
HTEXTURE ArtNum::m_tex=0;
std::string ArtNum::m_page_name="NumDic";
int ArtNum::m_couts=0;
ArtNum::ArtNum(void):
m_x(0.0f),
m_y(0.0f),
m_w(20.0f),
m_h(40.0f)
{
	m_w=0.02f*1024;
	m_h=0.04f*768;
	m_couts++;
	if (!b_load_dic)
	{
		//load dic
		m_dic.clear();
		using namespace Utility;
		//page_info_getter::Instance().add_page("./rez/Screen/NumDic.txt",m_page_name);
		//load numbs
		for (int i=0;i<=9;++i)
		{
			ArtNum_Uvs tempuv;
			char tempstr[8];
			sprintf(tempstr,"n%d",i);
			tempuv.u=page_info_getter::Instance().get<float>(m_page_name,tempstr,"u");
			tempuv.v=page_info_getter::Instance().get<float>(m_page_name,tempstr,"v");
			tempuv.mu=page_info_getter::Instance().get<float>(m_page_name,tempstr,"umax");
			tempuv.mv=page_info_getter::Instance().get<float>(m_page_name,tempstr,"vmax");
			m_dic.insert(std::make_pair(tempstr,tempuv));
		}
		//load punctuations
		for (int i=1;i<=5;++i)
		{
			ArtNum_Uvs tempuv;
			char tempstr[8];
			sprintf(tempstr,"b%d",i);
			tempuv.u=page_info_getter::Instance().get<float>(m_page_name,tempstr,"u");
			tempuv.v=page_info_getter::Instance().get<float>(m_page_name,tempstr,"v");
			tempuv.mu=page_info_getter::Instance().get<float>(m_page_name,tempstr,"umax");
			tempuv.mv=page_info_getter::Instance().get<float>(m_page_name,tempstr,"vmax");
			m_dic.insert(std::make_pair(tempstr,tempuv));
		}
		if (m_tex==0)
		{
			g_pLTCTexInterface->CreateTextureFromName(m_tex,"./Screen/textures/numdic.dtx");
		}
		
		b_load_dic=true;
	}
	m_num_list.clear();
	m_num_list.reserve(10);
	changeValue(0);
	m_r=m_g=m_b=m_a=255;

}

ArtNum::~ArtNum(void)
{
	m_couts--;
	if (m_couts==0)
	{
		g_pLTCTexInterface->ReleaseTextureHandle(m_tex);
		b_load_dic=0;
	}

}

void ArtNum::render()
{
	
	g_pLTCDrawPrim->SetTexture(m_tex);
	//for (std::vector<LT_POLYFT4>::iterator it=m_num_list.begin();
	//	it!=m_num_list.end();++it)
 //   {
		g_pLTCDrawPrim->DrawPrim(&m_num_list.front(),m_num_list.size());
  //  }

	

}

void ArtNum::changeValue( int num )
{
	if (num==m_num)
	{
		return;
	}
	m_num_list.clear();
	m_num=num;
	int times;
	for (times=1;;times*=10)
	{
		if (num<times)
		{
			times/=10;
			break;
		}
	}
	int tempi=0;
	for (;;)
	{
		int num_value;
		if (times==0)
		{
			num_value=num;
		}
		else
		{
			num_value=num/times;
			num%=times;
		}
		
		char tempstr[8];
		sprintf(tempstr,"n%d",num_value);
		LT_POLYFT4 temp_poly;
		temp_poly.verts[0].u=m_dic[tempstr].u;
		temp_poly.verts[0].v=m_dic[tempstr].v;
		temp_poly.verts[1].u=m_dic[tempstr].mu;
		temp_poly.verts[1].v=m_dic[tempstr].v;
		temp_poly.verts[2].u=m_dic[tempstr].mu;
		temp_poly.verts[2].v=m_dic[tempstr].mv;
		temp_poly.verts[3].u=m_dic[tempstr].u;
		temp_poly.verts[3].v=m_dic[tempstr].mv;

		temp_poly.verts[0].x=m_x+tempi*m_w*1.05f;
		temp_poly.verts[0].y=m_y;
		temp_poly.verts[1].x=m_x+m_w+tempi*m_w*1.05f;
		temp_poly.verts[1].y=m_y;
		temp_poly.verts[2].x=m_x+m_w+tempi*m_w*1.05f;
		temp_poly.verts[2].y=m_y+m_h;
		temp_poly.verts[3].x=m_x+tempi*m_w*1.05f;
		temp_poly.verts[3].y=m_y+m_h;

		temp_poly.rgba.r=m_r;
		temp_poly.rgba.g=m_g;
        temp_poly.rgba.b=m_b;
		temp_poly.rgba.a=m_a;

		m_num_list.push_back(temp_poly);
		
		if (times<=1)
		{
			break;
		}else
			times/=10;
		    tempi++;
	}


}     

void ArtNum::SetPos( float x,float y )
{
	m_x=x;
	m_y=y;
	Update();

}

void ArtNum::SetSize( float w,float h )
{
	m_w=w;
	m_h=h;
	Update();

}

void ArtNum::Update()
{
	int num=m_num;
	m_num_list.clear();
	int times;
	for (times=1;;times*=10)
	{
		if (num<times)
		{
			times/=10;
			break;
		}
	}
	int tempi=0;
	for (;;)
	{
		int num_value;
		if (times==0)
		{
			num_value=num;
		}
		else
		{
			num_value=num/times;
			num%=times;
		}

		char tempstr[8];
		sprintf(tempstr,"n%d",num_value);
		LT_POLYFT4 temp_poly;
		temp_poly.verts[0].u=m_dic[tempstr].u;
		temp_poly.verts[0].v=m_dic[tempstr].v;
		temp_poly.verts[1].u=m_dic[tempstr].mu;
		temp_poly.verts[1].v=m_dic[tempstr].v;
		temp_poly.verts[2].u=m_dic[tempstr].mu;
		temp_poly.verts[2].v=m_dic[tempstr].mv;
		temp_poly.verts[3].u=m_dic[tempstr].u;
		temp_poly.verts[3].v=m_dic[tempstr].mv;

		temp_poly.verts[0].x=m_x+tempi*m_w*1.1f;
		temp_poly.verts[0].y=m_y;
		temp_poly.verts[1].x=m_x+m_w+tempi*m_w*1.1f;
		temp_poly.verts[1].y=m_y;
		temp_poly.verts[2].x=m_x+m_w+tempi*m_w*1.1f;
		temp_poly.verts[2].y=m_y+m_h;
		temp_poly.verts[3].x=m_x+tempi*m_w*1.1f;
		temp_poly.verts[3].y=m_y+m_h;

		temp_poly.rgba.r=m_r;
		temp_poly.rgba.g=m_g;
		temp_poly.rgba.b=m_b;
		temp_poly.rgba.a=m_a;

		m_num_list.push_back(temp_poly);

		if (times<=1)
		{
			break;
		}else
			times/=10;
		tempi++;
	}

}

void ArtNum::SetColor( uint8 r/*=255*/,uint8 g/*=255*/,uint8 b/*=255*/,uint8 a/*=255*/ )
{
	m_r=r;
	m_g=g;
	m_b=b;
	m_a=a;
	Update();

}

void ArtNum::changePunc( int num )
{
	if (num<1||num>5)
	{
		return;
	}
	m_num_list.clear();
	char tempstr[8];
	sprintf(tempstr,"b%d",num);
	LT_POLYFT4 temp_poly;
	temp_poly.verts[0].u=m_dic[tempstr].u;
	temp_poly.verts[0].v=m_dic[tempstr].v;
	temp_poly.verts[1].u=m_dic[tempstr].mu;
	temp_poly.verts[1].v=m_dic[tempstr].v;
	temp_poly.verts[2].u=m_dic[tempstr].mu;
	temp_poly.verts[2].v=m_dic[tempstr].mv;
	temp_poly.verts[3].u=m_dic[tempstr].u;
	temp_poly.verts[3].v=m_dic[tempstr].mv;

	temp_poly.verts[0].x=m_x;
	temp_poly.verts[0].y=m_y;
	temp_poly.verts[1].x=m_x+m_w;
	temp_poly.verts[1].y=m_y;
	temp_poly.verts[2].x=m_x+m_w;
	temp_poly.verts[2].y=m_y+m_h;
	temp_poly.verts[3].x=m_x;
	temp_poly.verts[3].y=m_y+m_h;

	temp_poly.rgba.r=m_r;
	temp_poly.rgba.g=m_g;
	temp_poly.rgba.b=m_b;
	temp_poly.rgba.a=m_a;

	m_num_list.push_back(temp_poly);
	 
}